<?php

namespace App\Admin\Controllers\User;

use App\Admin\Actions\SubTableAction;
use App\Models\User\LoginLog;
use App\Models\User\User;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Repositories\EloquentRepository;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class UserController extends AdminController
{
    /**
     * @var $repository EloquentRepository
     */
    protected $repository;

    public function __construct()
    {
        $this->repository = new EloquentRepository(User::class);

        Admin::translator()->setPath("user");
    }


    public function title()
    {
        return admin_trans_label("register-user");
    }


    public function index(Content $content)
    {
        return $content
            ->translation($this->translation())
            ->title($this->title())
            ->description($this->description()['index'] ?? trans('admin.list'))
            /*->body(function (Row $row){
                $row->column(4, new TotalUsers());
                $row->column(4, new NewUsers());
                $row->column(4, new NewDevices());
            })*/
            ->body($this->grid());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make($this->repository , function (Grid $grid) {
            #筛选
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();

                //如果存在条件就展开搜索框，否则点击才会显示
                if(!empty(request()->all())){
                    $filter->expand(true);
                }

                $filter->equal('username')->width(2);
                $filter->equal('state')->select(["0"=>"冻结","1"=>"正常"])->width(2);
                $filter->equal('gender')->select(["0"=>"保密","1"=>"男","-1"=>"女"])->width(2);
                $filter->equal('email')->width(2);
                $filter->equal('nation_code')->width(2);
                $filter->equal('mobile_phone')->width(2);
                $filter->between("registered_at")->datetime()->width(3);
            });

            $grid->column('id')->sortable();
            $grid->column('avatar')->image("","",35);
            $grid->column('username',admin_trans_field("username"));
            $grid->column('gender')->radio(["0"=>"保密","1"=>"男","-1"=>"女"]);
            $grid->column('email');
            #$grid->column('nation_code');
            $grid->column('mobile_phone',admin_trans_field("mobile_phone"))->display(function ($value) {
                return sprintf("+%d %d",$this->nation_code,$this->mobile_phone);
            });
            $grid->column('last_login_ip',admin_trans_field("last_login_ip"))->display(function ($value){
                return long2ip($value);
            });
            $grid->column('last_login_at');
            $grid->column('registered_at');
            $grid->column('state')->switch();
            $grid->column('created_at')->sortable();


            $grid->actions(function (Grid\Displayers\Actions $actions){
                //$actions->prepend(new SubTableAction('&nbsp;&nbsp;<i title="用户详情" class="feather icon-user" icon></i>&nbsp;&nbsp;',admin_url("user",$actions->getKey())));
                $actions->prepend(new SubTableAction('&nbsp;&nbsp;详情&nbsp;&nbsp;',admin_url("user",$actions->getKey())));
                //$actions->prepend(new SubTableAction('&nbsp;&nbsp;<i title="登录日志" class="feather icon-camera"></i>&nbsp;&nbsp;',admin_url(sprintf("user/%d/log/%s",$actions->getKey(),'login'))));
                $actions->prepend(new SubTableAction('&nbsp;&nbsp;日志</i>&nbsp;&nbsp;',admin_url(sprintf("user/%d/log/%s",$actions->getKey(),'login'))));
            });

            $grid->fixColumns(3,-3);

            $grid->disableViewButton();
            $grid->disableCreateButton();
            $grid->disableEditButton();
            $grid->disableDeleteButton();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make($this->repository, function (Form $form) {
            $form->display('id');
            $form->text('username');
            $form->text('avatar');
            $form->text('gender');
            $form->text('email');
            $form->text('nation_code');
            $form->text('mobile_phone');
            $form->text('password');
            $form->text('state');
            $form->text('last_login_ip');
            $form->text('last_login_at');
            $form->text('registered_at');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }

    public function show($id, Content $content)
    {
        return $content
            ->translation($this->translation())
            ->title($this->title())
            ->description($this->description()['show'] ?? trans('admin.show'))
            ->body($this->detail($id))
            ->body("<br/>")
            ->body($this->other($id))
            ;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, $this->repository, function (Show $show) use($id){

            $show->tools(function (Show\Tools $tools) use($id){
                $tools->disableList();
                $url = admin_url(sprintf("user/%d/log/%s",$id,'login'));
                $tools->append(<<<HTML
                <div class="pull-left" data-responsive-table-toolbar="grid-table" style="margin:0 5px">
                    <a href="{$url}" class="btn btn-primary"><i class="feather icon-layers"></i><span class="d-none d-inline">&nbsp;&nbsp;登录日志</span></a>
                 </div>
HTML);
            });

            $show->row(function (Show\Row $row){
                $row->width(4)->field("id","ID");
                $row->width(4)->field("gender","性别")->using(["0"=>"保密","1"=>"男","-1"=>"女"]);
                $row->width(4)->field("state","状态")->using(["0"=>"冻结","1"=>"正常"]);
            });
            $show->row(function (Show\Row $row){
                $row->width(4)->field("nation_code","区号");
                $row->width(4)->field("mobile_phone","手机");
                $row->width(4)->field("email","Email");
            });

            $show->row(function (Show\Row $row){
                $row->width(4)->field("registered_at","注册时间");
                $row->width(4)->field("last_login_at","登录时间");
                $row->width(4)->field("last_login_ip","登录ip");
                #$row->width(3)->field("created_at");
                #$row->width(3)->field("updated_at");
            });

            /*$show->relation("logs",function ($model){
                return $this->logs($model->id);
            });*/

            $show->disableDeleteButton();
            $show->disableEditButton();
            $show->disableQuickEdit();
        });
    }

    /**
     * 这里只是一个例子，实际上应该是放一些义务相关的表单和详情
     * @param $id
     * @return Grid
     */
    public function other($id)
    {
        $repository = new EloquentRepository(LoginLog::where("uid",$id));

        return Grid::make($repository , function (Grid $grid) {

            $grid->column('id')->sortable();
            $grid->column('login_from');
            $grid->column('login_result');
            $grid->column('login_message');
            $grid->column('login_ip')->display(function ($value){
                return long2ip($value);
            });
            $grid->column('os');
            $grid->column('created_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
            $grid->disableCreateButton();
            $grid->disableEditButton();
            $grid->disableDeleteButton();
            $grid->disableActions();
            $grid->disableFilterButton();
        });
    }


}
